Keyboard Row
LeetCode 500 | Difficulty: Easyβ
EasyProblem Descriptionβ
Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.
Note that the strings are case-insensitive, both lowercased and uppercased of the same letter are treated as if they are at the same row.
In the American keyboard:
- the first row consists of the characters `"qwertyuiop"`,
- the second row consists of the characters `"asdfghjkl"`, and
- the third row consists of the characters `"zxcvbnm"`.

Example 1:
Input: words = ["Hello","Alaska","Dad","Peace"]
Output: ["Alaska","Dad"]
Explanation:
Both "a" and "A" are in the 2nd row of the American keyboard due to case insensitivity.
Example 2:
Input: words = ["omk"]
Output: []
Example 3:
Input: words = ["adsdf","sfd"]
Output: ["adsdf","sfd"]
Constraints:
- `1 <= words.length <= 20`
- `1 <= words[i].length <= 100`
- `words[i]` consists of English letters (both lowercase and uppercase).
Topics: Array, Hash Table, String
Approachβ
Hash Mapβ
Use a hash map for O(1) average lookups. Store seen values, frequencies, or indices. The key question: what should I store as key, and what as value?
Need fast lookups, counting frequencies, finding complements/pairs.
String Processingβ
Consider character frequency counts, two-pointer approaches, or building strings efficiently. For pattern matching, think about KMP or rolling hash. For palindromes, expand from center or use DP.
Anagram detection, palindrome checking, string transformation, pattern matching.
Solutionsβ
Solution 1: C# (Best: 228 ms)β
| Metric | Value |
|---|---|
| Runtime | 228 ms |
| Memory | 42.9 MB |
| Date | 2022-02-02 |
public class Solution {
public string[] FindWords(string[] words) {
string option1 = "qwertyuiop";
string option2 = "asdfghjkl";
string option3 = "zxcvbnm";
List<string> result = new List<string>();
foreach (var word in words)
{
var word1 = word.ToLower();
if (!word1.Where(x => !option1.Contains(x)).Any() ||
!word1.Where(x => !option2.Contains(x)).Any() ||
!word1.Where(x => !option3.Contains(x)).Any())
result.Add(word);
}
return result.ToArray();
}
}
π 1 more C# submission(s)
Submission (2022-02-02) β 315 ms, 42.9 MBβ
public class Solution {
public string[] FindWords(string[] words) {
string option1 = "qwertyuiop";
string option2 = "asdfghjkl";
string option3 = "zxcvbnm";
List<string> result = new List<string>();
foreach (var word in words)
{
var word1 = word.ToLower();
if (word1.Where(x => !option1.Contains(x)).ToList().Count == 0) result.Add(word);
if (word1.Where(x => !option2.Contains(x)).ToList().Count == 0) result.Add(word);
if (word1.Where(x => !option3.Contains(x)).ToList().Count == 0) result.Add(word);
}
return result.ToArray();
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Hash Map | $O(n)$ | $O(n)$ |
Interview Tipsβ
- Start by clarifying edge cases: empty input, single element, all duplicates.
- Hash map gives O(1) lookup β think about what to use as key vs value.